//
//  SocialReleaseToolView.m
//  ShowYork
//
//  Created by hxh on 2020/9/24.
//  Copyright © 2020 hxh. All rights reserved.
//

#define itemH 60
#define itemLine 8

typedef void(^doneBlock)(NSInteger index);
#import "MinePhotoFooterView.h"

@interface MinePhotoFooterView ()
@property (nonatomic,strong)UIButton *photoBtn;
@property (nonatomic,strong)UIButton *videoBtn;
@property (nonatomic,strong)UIButton *cancelBtn;
@property (nonatomic,strong)UIView *containView;
@property (nonatomic,strong)UIView *grayLine;
@property (nonatomic,strong)UIView *midLine;
@property (nonatomic,strong)UIView *bottomView;
@property (nonatomic,strong)UILabel *videoMainTitleLb;
@property (nonatomic,strong)UILabel *videoSubTitleLb;
@property (nonatomic,strong)UILabel *photoTitleLb;
@property (nonatomic,strong)UILabel *cancelLb;
@property (nonatomic,copy)doneBlock done;

@end

@implementation MinePhotoFooterView

- (instancetype)initWithCompleteBlock:(void (^)(NSInteger))completeBlock{
    if (self = [super init]) {
        [self setUI];
        if (completeBlock) {
            self.done = ^(NSInteger index) {
                completeBlock(index);
            };
        }
    }
    return self;
}


- (void)setUI{
    self.frame = CGRectMake(0, 0, kScreenW, kScreenH);
    self.backgroundColor = RGBA(0, 0, 0, 0.4);
    
    self.containView = [[UIView alloc]init];
    [self addSubview:self.containView];

    [self.containView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.bottom.mas_equalTo(self).offset(itemH * 3 + itemLine + GK_SAVEAREA_BTM);
        make.height.mas_equalTo(itemH * 3 + itemLine + GK_SAVEAREA_BTM);
    }];
    
    self.videoBtn = [[UIButton alloc]init];
    self.videoBtn.backgroundColor = [UIColor whiteColor];
    [self.videoBtn addTarget:self action:@selector(videoMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.containView addSubview:self.videoBtn];
    
    [self.videoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.containView);
        make.height.mas_equalTo(60);
    }];
    
    self.videoMainTitleLb = [[UILabel alloc]init];
    self.videoMainTitleLb.text = @"拍照";
    self.videoMainTitleLb.textColor = RGBA(51, 51, 51, 1);
    self.videoMainTitleLb.font = font(18);
    [self.containView addSubview:self.videoMainTitleLb];
    [self.videoMainTitleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.videoBtn);
        make.centerY.equalTo(self.videoBtn);
    }];


    self.photoBtn = [[UIButton alloc]init];
    self.photoBtn.backgroundColor = [UIColor whiteColor];
    [self.photoBtn addTarget:self action:@selector(photoMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.containView addSubview:self.photoBtn];
    [self.photoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.containView);
        make.top.equalTo(self.videoBtn.mas_bottom);
        make.height.mas_equalTo(60);
    }];
    
    self.photoTitleLb = [[UILabel alloc]init];
    self.photoTitleLb.text = @"从手机相册中选择";
    self.photoTitleLb.textColor = RGBA(51, 51, 51, 1);
    self.photoTitleLb.font = font(18);
    [self.containView addSubview:self.photoTitleLb];
    
    [self.photoTitleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.photoBtn);
    }];
    
    self.grayLine = [[UIView alloc]init];
    self.grayLine.backgroundColor = RGBA(248, 248, 248, 248);
    [self.containView addSubview:self.grayLine];
    [self.grayLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.photoBtn.mas_bottom);
        make.left.right.equalTo(self.containView);
        make.height.mas_equalTo(8);
    }];
    
    self.cancelBtn = [[UIButton alloc]init];
    self.cancelBtn.backgroundColor = [UIColor whiteColor];
    [self.cancelBtn addTarget:self action:@selector(cancelMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.containView addSubview:self.cancelBtn];
    [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.containView);
        make.top.equalTo(self.grayLine.mas_bottom);
        make.height.mas_equalTo(60);
    }];
    
    self.bottomView = [[UIView alloc]init];
    self.bottomView.backgroundColor = [UIColor whiteColor];
    [self.containView addSubview:self.bottomView];
    
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.containView);
        make.top.equalTo(self.cancelBtn.mas_bottom);
    }];
    
    self.cancelLb = [[UILabel alloc]init];
    self.cancelLb.text = @"取消";
    self.cancelLb.textColor = RGBA(51, 51, 51, 1);
    self.cancelLb.font = font(18);
    [self.containView addSubview:self.cancelLb];
    
    [self.cancelLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.cancelBtn);
    }];
 
    self.midLine = [[UIView alloc]init];
    self.midLine.backgroundColor = RGBA(230, 230, 230, 1);
    [self.containView addSubview:self.midLine];
    
    [self.midLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.containView).offset(16);
        make.right.equalTo(self.containView).offset(-16);
        make.height.mas_equalTo(0.5);
        make.top.equalTo(self.videoBtn.mas_bottom);
    }];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cancelMethod)];
    [self addGestureRecognizer:tap];
}


- (void)videoMethod{
    [self cancelMethod];
    if (self.done) {
        self.done(1);
    }
}
- (void)photoMethod{
   
    [self cancelMethod];
    
    if (self.done) {
        self.done(2);
    }
}
- (void)cancelMethod{
    [self.containView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self).offset(itemH * 3 + itemLine + GK_SAVEAREA_BTM);
    }];
    [UIView animateWithDuration:0.25 animations:^{
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        if (finished) {
            [self removeFromSuperview];
        }
    }];
}

- (void)show{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [self layoutIfNeeded];
    [self.containView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self);
    }];
    [UIView animateWithDuration:0.25 animations:^{
        [self layoutIfNeeded];
    }];
}

- (void)drawRect:(CGRect)rect{
    [super drawRect:rect];

    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.videoBtn.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(14, 14)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.videoBtn.bounds;
    maskLayer.path = maskPath.CGPath;
    self.videoBtn.layer.mask = maskLayer;
    
    
}

@end
